#ifndef _ADC_C_
#define _ADC_C_

#include "include/adc.h"
#include "include/ca51f003_config.h"		
#include "include/ca51f003sfr.h"
#include "include/ca51f003xsfr.h"
#include "include/gpiodef_f003.h"


void ADC_Init()
{
	/**********配置ADC引脚功能*************************************************************/
	P03F = P03_ADC3_SETTING;	//设置P03为ADC引脚功能
	P11F = P11_ADC1_SETTING;	//设置P11为ADC引脚功能
/**********初始化ADC寄存器（设置ADC时钟、选择ADC参考电压、设置ADC运放）***********/
	ADCON = AST(0) | ADIE(0) | HTME(7) | VSEL(ADC_REF_VDD);			//设置ADC参考电压为VDD
	ADOPC = GAIN(NO_AMP);
}

unsigned int Get_AdcValue(char Adc_Channelx)
{
	unsigned int Adc_value = 0;
	ADCFGL = ACKD(2) | ADCALE(1) | ADCHS(Adc_Channelx);	//选择ADC1通道
	ADCON |= AST(1);											//启动ADC转换
	while(!(ADCON & ADIF));								//等待ADC转换完成
	ADCON |= ADIF;												//清除ADC中断标志
	Adc_value = ADCDH*256 + ADCDL;			//读取AD值
	Adc_value >>= 4;
	return Adc_value;
}


#endif
